package com.struggle.rabbitmqconsumer.config;

import com.struggle.rabbitmqconsumer.listener.direct.DirectReceiver;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @className MessageListenerConfig
 * @Description 消息监听配置类，开启消息手动确认
 * @Author liuwj3
 * @Date 2019/10/14 0014 上午 11:43
 **/
@Configuration
public class MessageListenerConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private DirectReceiver directReceiver;

    @Autowired
    private DirectRabbitConfig directRabbitConfig;

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        // 允许以并行方式处理消息，以加速整体执行。
        // 通过这样做，你会自动唱出一个合同，你接受一切都是异步的，你不能再承担秩序了
        /*container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(1);*/
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setQueues(directRabbitConfig.getDirectQueue());
        // directReceiver需要实现ChannelAwareMessageListener接口
        container.setMessageListener(directReceiver);
        return container;
    }

    /*@Bean(name = "singleListenerContainer")
    public SimpleRabbitListenerContainerFactory listenerContainer(){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(1);
        factory.setPrefetchCount(1);
        factory.setTxSize(1);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }*/
}
